package com.example.imtoken.view.function

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.imtoken.R
import com.example.imtoken.core.OnMultifunctionalSelectorListener
import com.example.imtoken.databinding.MultifunctionalSelectorDialogFragmentBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.xuexiang.xui.adapter.recyclerview.DividerItemDecoration

/**
 * 使用 [AddressActivity::class.java]
 * 多功能抽屉
 * @property viewModel MultifunctionalSelectorDialogViewModel
 * @property dataBinding MultifunctionalSelectorDialogFragmentBinding
 * @property mBehavior BottomSheetBehavior<View>?
 * @property adapter MultifunctionalSelectorDialogAdapter?
 * @property data MutableList<String>?
 * @property onMultifunctionalSelectorListener OnMultifunctionalSelectorListener?
 */
class MultifunctionalSelectorDialogFragment : BottomSheetDialogFragment() {

    private val viewModel: MultifunctionalSelectorDialogViewModel by lazy {
        ViewModelProvider(this).get(MultifunctionalSelectorDialogViewModel::class.java)
    }

    private lateinit var dataBinding: MultifunctionalSelectorDialogFragmentBinding

    private var mBehavior: BottomSheetBehavior<View>? = null

    private var adapter: MultifunctionalSelectorDialogAdapter? = null

    private var data: MutableList<String>? = null

    private var onMultifunctionalSelectorListener: OnMultifunctionalSelectorListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //重写样式,设置dialog背景色为透明
        setStyle(STYLE_NORMAL, R.style.AppBottomSheet)
    }

    @SuppressLint("ResourceType")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dataBinding =
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.multifunctional_selector_dialog_fragment,
                null,
                false
            )
        dialog.setContentView(dataBinding.root)
//        val lp = dataBinding.root.layoutParams
//        lp.height = ScreenUtils.getAppScreenHeight() - ConvertUtils.sp2px(50f)
//        dataBinding.root.layoutParams = lp
        mBehavior = BottomSheetBehavior.from(dataBinding.root.parent as View)
        initViewContent()
        return dialog
    }

    private fun initViewContent() {
        dataBinding.tvCancel.setOnClickListener { dismiss() }
        if (adapter == null) {
            adapter = MultifunctionalSelectorDialogAdapter(R.layout.item_multifunctional)
            val layoutManager = LinearLayoutManager(activity)
            dataBinding.recyclerView.layoutManager = layoutManager
            dataBinding.recyclerView.addItemDecoration(
                DividerItemDecoration(
                    activity,
                    LinearLayoutManager.VERTICAL
                )
            )
            dataBinding.recyclerView.adapter = adapter
            adapter?.setOnItemClickListener { _, _, position ->
                onMultifunctionalSelectorListener?.selectPosition(position)
                dismiss()
            }
        }
        adapter?.setList(data)
    }

    fun setData(
        strList: MutableList<String>,
        onMultifunctionalSelectorListener: OnMultifunctionalSelectorListener
    ) {
        this.data = strList
        this.onMultifunctionalSelectorListener = onMultifunctionalSelectorListener
    }

    override fun onStart() {
        super.onStart()
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    }

}